<template>
  <div>
    <div id="mychart-pie" ref="pie"></div>
  </div>
</template>

<script>
export default {
  props: ["isLight", "option"],
  data() {
    return {
      chartBox: null,
      timer: null,
    };
  },
  methods: {
    // 基本柱形图
    change() {
      this.chartBox.setOption(this.option);
      this.lightFun();
      // 根据页面大小自动响应图表大小
      window.addEventListener("resize", () => {
        this.chartBox.resize();
      });
    },
    // 高亮函数
    lightFun() {
      let currentIndex = -1;
      if (this.isLight) {
        this.timer = setInterval(() => {
          let dataLen = this.option.series[0].data.length;
          // 取消之前高亮的图形
          this.chartBox.dispatchAction({
            type: "downplay",
            seriesIndex: 0,
            dataIndex: currentIndex,
          });
          currentIndex = (currentIndex + 1) % dataLen;
          // 高亮当前图形
          this.chartBox.dispatchAction({
            type: "highlight",
            seriesIndex: 0,
            dataIndex: currentIndex,
          });
          // 显示 tooltip
          this.chartBox.dispatchAction({
            type: "showTip",
            seriesIndex: 0,
            dataIndex: currentIndex,
          });
        }, 3000);
      }
    },
  },
  mounted() {
    // this.chartBox = this.$echarts.init(document.getElementById("mychart-pie"));
    this.chartBox = this.$echarts.init(this.$refs.pie);
    this.change();
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
};
</script>

<style lang="scss" scoped>
#mychart-pie {
  width: 100%;
  height: 100%;
}
</style>
